<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>排序</title>
</head>

<body>
    <script>
        var nums = [30, 18, 20, 56, 22];
        for (var i = 0; i < nums.length; i++) {
            for (j = 0; j < nums.length - i; j++) {
                if (nums[j] > nums[j + 1]) {//相邻元素两两对比
                    var tmp = nums[j];  //元素交换
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                }
            }
        }
        console.log(nums);
    </script>
</body>

</html>